<template>
  <el-card>
    <template #header>
      <div class="card-header">
        <span>配套信息管理</span>
      </div>
    </template>
    <el-row class="container" v-for="(item,index) in facilityTypeList" :key="index">
      <el-col :span="2" class="text-center">{{ item.label }}配套</el-col>
      <el-col :span="22" class="item-container">
        <el-popconfirm
          v-for="item2 in item.children || []"
          :key="item2.id"
          width="220"
          confirm-button-text="删除"
          cancel-button-text="修改"
          cancel-button-type="warning"
          @confirm="deleteFacilityHandle(item2)"
          @cancel="editFacilityHandle(item2, item)"
          :title="`修改或删除${item2.name}`"
        >
          <template #reference>
            <div class="item m-r-2 pointer">
              <svg-icon className="attribute-icon" :icon-class="`rent-${item2.icon}`" />
              <span>{{ item2.name }}</span>
            </div>
          </template>
        </el-popconfirm>
        <el-icon class="item m-r-2 pointer" :size="35" color="#409EFF" @click="addFacilityHandle(item)">
          <CirclePlus />
        </el-icon>
      </el-col>
    </el-row>
    <!--    配套信息修改弹窗管理-->
    <el-dialog :title="dialog.title" v-model="dialog.visible" :destroy-on-close="true" style="max-width: 700px">
      <el-form ref="facilityInfoFormRef" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="配套名称" prop="name">
          <el-input v-model.trim="form.name" autocomplete="off" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="配套图标" prop="icon">
          <rent-icon-select v-model="form.icon" class="!w-100%" />
        </el-form-item>
        <el-form-item label="排序号" prop="sort">
          <el-input-number v-model="form.sort" :min="0" :step="10" class="!w-100%" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </el-card>
</template>
<script setup lang="ts">
import { FacilityInfoVO, FacilityInfoQuery, FacilityInfoForm } from '@/api/apartment/attribute/facilityInfo/types';
import {
  listFacilityInfo,
  getFacilityInfo,
  delFacilityInfo,
  addFacilityInfo,
  updateFacilityInfo
} from '@/api/apartment/attribute/facilityInfo/index';
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const queryFormRef = ref<ElFormInstance>();
const facilityInfoFormRef = ref<ElFormInstance>();
const buttonLoading = ref(false);
const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});
type SupportFacilityInterface = {
  label: string;
  value: any;
  children?: FacilityInfoVO[];
};

const initFormData: FacilityInfoForm = {
  id: undefined,
  type: undefined,
  name: undefined,
  icon: undefined,
  sort: undefined
};
const data = reactive<PageData<FacilityInfoForm, FacilityInfoQuery>>({
  form: { ...initFormData },
  queryParams: {
    pageNum: 1,
    pageSize: 500,
    type: undefined,
    name: undefined,
    icon: undefined,
    sort: undefined,
    params: {}
  },
  rules: {
    name: [{ required: true, message: '配套名称不能为空', trigger: 'blur' }],
    icon: [{ required: true, message: '配套图标不能为空', trigger: 'blur' }],
    sort: [{ required: true, message: '排序号不能为空', trigger: 'blur' }]
  }
});

const { queryParams, form, rules } = toRefs(data);

const BuildingTypeTypeMap = [
  { label: '公寓', value: 1 },
  { label: '房间', value: 2 }
];
// 配套类型
const facilityTypeList = ref<SupportFacilityInterface[]>([]);
// 获取配套信息
const getFacilityInfoListHandle = async () => {
  const { data } = await listFacilityInfo(queryParams.value);
  facilityTypeList.value = JSON.parse(JSON.stringify(BuildingTypeTypeMap));
  data.forEach((item) => {
    // 获取对应类型的名称
    const targetType = facilityTypeList.value.find((item2) => item2.value === item.type);
    if (targetType) {
      targetType.children = targetType.children || [];
      targetType.children.push(item);
    }
  });
};
/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
};
/** 表单重置 */
const reset = () => {
  form.value = { ...initFormData };
  facilityInfoFormRef.value?.resetFields();
};
// 删除配套
const deleteFacilityHandle = async (item: FacilityInfoVO) => {
  await delFacilityInfo(item.id);
  await getFacilityInfoListHandle();
  proxy?.$modal.msgSuccess('操作成功');
};
// 编辑配套
const editFacilityHandle = async (row?: FacilityInfoVO, item?: SupportFacilityInterface) => {
  const _id = row?.id;
  const res = await getFacilityInfo(_id);
  Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = '修改' + item.label + '配套信息';
};
// 添加配套
const addFacilityHandle = (item: SupportFacilityInterface) => {
  reset();
  dialog.visible = true;
  dialog.title = '添加' + item.label + '配套信息';
  form.value.type = item.value;
};
/** 提交按钮 */
const submitForm = () => {
  facilityInfoFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id) {
        await updateFacilityInfo(form.value).finally(() => (buttonLoading.value = false));
      } else {
        await addFacilityInfo(form.value).finally(() => (buttonLoading.value = false));
      }
      proxy?.$modal.msgSuccess('操作成功');
      dialog.visible = false;
      await getFacilityInfoListHandle();
    }
  });
};
onMounted(() => {
  getFacilityInfoListHandle();
});
</script>

<style scoped lang="scss">
.card-header {
  font-size: 18px;
  font-weight: bold;
}

.text-center {
  display: flex;
  align-items: center;
  justify-content: flex-start;
  text-align: center;
}

.container:not(:first-child) {
  margin-top: 20px;
}

.item-container {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: flex-start;
  width: 100%;
  padding: 10px 15px;
  background-color: #efefef;
  border-radius: 20px;

  .item {
    position: relative;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    width: 50px;
  }
}
</style>
